package com.zhongfl.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.zhongfl.crm.event.LxChannelCreateEvent;
import com.zhongfl.crm.event.LxChannelUpdateEvent;
import com.zhongfl.crm.event.register.LxChannelCreateEventHandlerRegister;
import com.zhongfl.crm.event.register.LxChannelUpdateEventHandlerRegister;
import com.zhongfl.crm.mapper.LxChannelMapper;
import com.zhongfl.crm.model.LxChannel;
import com.zhongfl.crm.request.LxChannelRequest;
import com.zhongfl.crm.service.LxChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 励销渠道业务层
 * @author 犬冢牙
 * 2018/9/21 11:27
 */
@Slf4j
@Service
public class LxChannelServiceImpl implements LxChannelService {

    @Resource
    private LxChannelMapper lxChannelMapper;

    @Resource
    private LxChannelCreateEventHandlerRegister lxChannelCreateEventHandlerRegister;

    @Resource
    private LxChannelUpdateEventHandlerRegister lxChannelUpdateEventHandlerRegister;

    /**
     * 分页查询所有励销渠道数据
     * @return 分页查询的渠道数据
     */
    @Override
    public List<LxChannel> findAllLxChannelByPage(LxChannelRequest request) {

        PageHelper.startPage(request.getPage(),request.getPageSize());
        return lxChannelMapper.findByPage(request);
    }

    /**
     * 查询所有数据
     * @param request
     * @return  查询出的所有渠道数据
     */
    @Override
    public List<LxChannel> findAllLxChannel(LxChannelRequest request) {
        return lxChannelMapper.findByPage(request);
    }

    /**
     *  批量删除
     * @param LxChannelIds
     */
    /*@Override
    public void deleteByIds(List<Long> LxChannelIds) {
        lxChannelMapper.deleteByIds(LxChannelIds);
    }*/

    /**
     * 删除一条数据
     * @param lxChannelId
     */
    @Override
    public void deleteById(Long lxChannelId) {
        lxChannelMapper.deleteById(lxChannelId);
    }

    /**
     * 修改一条数据
     * @param lxChannel
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByLxId(LxChannel lxChannel) {
        lxChannelMapper.updateByLxId(lxChannel);
        List<LxChannelUpdateEvent> handlers = lxChannelUpdateEventHandlerRegister.getLxChannelUpdateEvents();
        log.info("励销渠道更新事件处理器 :{}", handlers);
        for (LxChannelUpdateEvent handler : handlers) {
            handler.handle(lxChannel);
        }
    }

    /**
     * 新增一条数据
     * @param lxChannel
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSelective(LxChannel lxChannel) {
        lxChannelMapper.insertSelective(lxChannel);
        List<LxChannelCreateEvent> handlers = lxChannelCreateEventHandlerRegister.getLxChannelCreateEvents();
        for (LxChannelCreateEvent handler : handlers) {
            handler.handle(lxChannel);
        }
    }

    /**
     * 按id查询一条数据
     * @param lxId
     * @return
     */
    @Override
    public LxChannel selectByLxId(Long lxId) {
        return lxChannelMapper.selectByLxId(lxId);
    }

}
